package com.base.controller;


import com.base.comm.CommContent;
import com.base.po.tbk.TbkBrandCategory;
import com.base.po.voip.Users;
import com.base.service.LogsService;
import com.base.service.TbkBrandCategoryService;
import com.base.service.UsersService;
import com.base.utils.IP;
import com.base.utils.Produce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class add_TbkBrandCategory {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private TbkBrandCategoryService tbkBrandCategoryService;
	@Autowired
	private UsersService usersService;

	// 添加一级分类
	@RequestMapping("/add_TbkBrandCategory")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		if (adminUserId == null) {
			modelAndView.setViewName("jsp/login");
			return modelAndView;
		}
        
		modelAndView.setViewName("jsp/add_TbkBrandCategory");
		return modelAndView;
	}
	
	
	String SUCCESS = "success";

	// 添加广告
	@RequestMapping(value = "/insertTbkBrandCategory", method = RequestMethod.POST)
	public void insertSelfShopCategory(HttpServletRequest request, HttpServletResponse response, 
			String agentid, String category, Integer level, String pic, String firstid,
			String topid, Byte datastatus) throws IOException {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminId == null) {
			out.print("请刷新页面重试！");
			return;
		}	
		Users users = usersService.selectByPrimaryKey(adminId);
		List<TbkBrandCategory> selfShopCategory = null;
		
		String id = Produce.getUUID();
		int resultCode = 0;
		//暂定一级分类
		level = 1;
		//设置二级分类
		if(firstid != null && !firstid.equals("undefined")) {
			level = 2;
			topid = firstid;
		}

		if(pic == null || pic.equals("")){
			pic = null;
		}

		if(users.getRole() == 1) {
			resultCode = tbkBrandCategoryService.insert(new TbkBrandCategory(id, adminId, adminId, category,
					level, pic, datastatus, Produce.getNowDate()));
		}
		if(users.getRole() == 2) {
			resultCode = tbkBrandCategoryService.insert(new TbkBrandCategory(id, users.getParentid(), adminId,
					category, level, pic, datastatus, Produce.getNowDate()));
		}
		
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		if (resultCode == 1) {
			logsService.insert(CommContent.TYPE_UPDATE, "添加"+category+"品牌专场分类信息", usernamesys,
					true, IP.getIpAddr(request));
			out.print(SUCCESS);
		} else {
			logsService.insert(CommContent.TYPE_UPDATE, "添加"+category+"品牌专场分类失败", usernamesys,
					true, IP.getIpAddr(request));
			out.print("更改失败，请您重试");
		}
	}

}
